﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using DataEntities;
using DataProvider;
using SQLDataProvider;
using System.Linq;

namespace BusinessServices
{
    public class XuZuService
    {
        private readonly IBaseRepository<XuZuInfo> _xuZuRepository;

        public XuZuService()
        {
            _xuZuRepository = new BaseRepository<XuZuInfo>();
        }

        /// <summary>
        /// 获取活动中的续租信息
        /// </summary>
        public List<XuZuInfo> GetActiveXuZuInfo()
        {
            return _xuZuRepository.GetAll(x => !x.bXZZF, JoinChuZuInfo).OrderByDescending(x => x.tNewDate).ToList();
        }

        /// <summary>
        /// 获取超期的续租信息
        /// </summary>
        public List<XuZuInfo> GetTimeOverXuZuInfo()
        {
            return _xuZuRepository.GetAll(x => !x.bXZZF && x.tNewDate <= DateTime.Now, JoinChuZuInfo);
        }

        /// <summary>
        /// 根据出租单编号获取续租信息
        /// </summary>
        /// <param name="czdId">出租单编号</param>
        public List<XuZuInfo> GetXuZuInfoByChuZuId(int czdId)
        {
            return _xuZuRepository.GetAll(x => x.iCZDId == czdId);
        }

        //Join出租单表
        private DataLoadOptions JoinChuZuInfo()
        {
            var loadOption = new DataLoadOptions();
            loadOption.LoadWith<XuZuInfo>(p => p.ChuZuInfo);
            return loadOption;
        }

        /// <summary>
        /// 添加续租信息
        /// </summary>
        public XuZuInfo Add(XuZuInfo xuZuInfo)
        {
            return _xuZuRepository.Add(xuZuInfo);
        }

        /// <summary>
        /// 更新指定出租单的所有续租信息
        /// </summary>
        /// <param name="czdId">出租单编号</param>
        /// <param name="update">包含更新的委托，在此委托中进行更新操作</param>
        /// <returns>返回更新后的数据集</returns>
        public List<XuZuInfo> Update(int czdId,Action<XuZuInfo> update)
        {
            return _xuZuRepository.UpdateAll(x => x.iCZDId == czdId, update);
        }
    }
}
